package com.v5ba.demo.extend;

import com.v5ba.demo.extend.vo.OrgService;
import com.v5ba.demo.extend.vo.UserService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @author yiren
 * @since 2022/7/27
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // 定义orgService
        RootBeanDefinition beanDefinition = new RootBeanDefinition(OrgService.class);
        registry.registerBeanDefinition("orgService", beanDefinition);
        // 定义userService，需要依赖orgService
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(UserService.class);
        builder.addAutowiredProperty("orgService");
        registry.registerBeanDefinition("userService", builder.getBeanDefinition());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("111111111111");
    }
}
